<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加</title>
    <#assign base=request.contextPath  />
    <base id="base" href="${base}">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, adminUser-scalable=0">
    <link rel="stylesheet" href="${base}/static/win10/component/layui/css/layui.css" media="all">
    <script>
        var base = "${base}";
    </script>
</head>
<body>
<br>
<form class="layui-form " action="">

    <div class="layui-row">
        <div class="layui-col-xs6">
            <fieldset class="layui-elem-field layui-field-title">
                <legend>所有结构</legend>
            </fieldset>
            <div id="test9" class="demo-tree demo-tree-box" style="width: 80%; height: 450px;"></div>
        </div>


        <div class="layui-col-xs6">
            <fieldset class="layui-elem-field layui-field-title">
                <legend>结构添加</legend>
            </fieldset>
            <div class="layui-form-item">
                <label class="layui-form-label">结构名字</label>
                <div class="layui-input-block">
                    <input type="text" id="structureName" name="structureName" autocomplete="off" placeholder="请输入结构名字"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">选择上级</label>
                <div class="layui-input-block">
                    <input type="hidden" name="parentId" id="parentId">
                    <input type="text" id="parentName" class="layui-input" readonly placeholder="请选择上级">
                </div>
            </div>
        </div>
    </div>

    <div align="center">
        <button  class="layui-btn" id="submit">添加</button>
        <button type="button" class="layui-btn" id="del">删除</button>
    </div>
</form>

<script src="${base}/static/win10/component/layui/layui.js?t=1"></script>
<script src="${base}/static/win10/js/jquery-1.8.3.min.js?t=1"></script>
<script>
    layui.use(['tree', 'form', 'layer'], function () {


        var form = layui.form,
            tree = layui.tree,
            layer = layui.layer;
        //获取树形结构数据
        var data1 = getTreeData();

        //渲染树形结构
        tree.render({
            elem: '#test9'
            , data: data1
            ,edit: ['add','del']
            , click: function (obj) {
                console.log(JSON.stringify(obj.data));
                //选中节点高亮
                var nodes = document.getElementsByClassName("layui-tree-txt");

                for (var i = 0; i < nodes.length; i++) {


                    if (nodes[i].innerHTML === obj.data.title)
                        nodes[i].style.color = "red";
                    else
                        nodes[i].style.color = "#555";
                }


                console.log("结构id" + obj.data.id)
                console.log("结构id" + obj.data.title)
                //赋值
                $("#parentId").val(obj.data.id);
                $("#parentName").val(obj.data.title);
            }
            ,operate: function (obj) {
                var type=obj.type;  //得到Tpe 类型
                var data = obj.data; //得到数据
                var elem=obj.elem;// 得到当前节点

                if(type==='add'){
                // alert('2222')
                //     console.log(obj)
                    layer.open({
                        id:1,
                        type: 1,
                        title:'请输入问题',
                        style: 'width:80%;height:auto;',
                        content: "<div style='display:flex;justify-content:center;'><input type=\"text\"  id='xiaoshoukai' style='width:80%;' name=\"title\" required lay-verify=\"required\" placeholder=\"请输入标题\" autocomplete=\"off\" class=\"layui-input\"></div>",
                        btn:['保存','取消'],
                        yes:function (index,layero) {
                            //获取输入框里面的值
                            var closeContent = top.$("#xiaoshoukai").val() || $("#xiaoshoukai").val();

                            if(closeContent){
                                console.log(closeContent);




                                var formDate = {
                                    structureName: closeContent,
                                    parentId: data.id
                                }

                                $.ajax({
                                    type: "POST",
                                    url: base + "/analyse/repairStructure/admin/addRepairStructure",
                                    data: formDate,
                                    success: function (data) {
                                        layer.msg(data.message)
                                        if (data.code === 200) {
                                            // window.location.href = base + "/analyse/repairStructure/admin/main.html"
                                            setTimeout(function () {


                                                window.location.reload();

                                            }, 1000)
                                        } else {

                                        }
                                    }
                                });




                            }
                            console.log('确定')
                            layer.close(index);
                            // 在这里提交数据
                        },
                        no:function (index,layero) {
                            console.log('取消')
                            layer.close(index);
                        }
                    });
                }

                if(type==='del'){
                    var idList = []

                    idList.push(data.id)


                    $.ajax({
                        type: "GET",
                        url: base + "/analyse/repairStructure/admin/delRepairStructure",
                        data:{"datas":idList},
                        dataType: "json",
                        success: function (data) {

                            if(data.code===200){
                                layer.msg(data.message)

                                window.location.reload();

                            }else{
                                layer.msg(data.message)
                            }
                        }
                    });
                }



            }
        });





        $("#edit").on("click", function () {
            if (check() === false) {
                return;
            }

            var formDate = {
                structureName: $("#structureName").val(),
                parentId: $("#parentId").val()
            }
            $.ajax({
                type: "POST",
                url: base + "/analyse/repairStructure/admin/editRepairStructure",
                data: formDate,
                success: function (data) {
                    layer.msg(data.message)
                    if (data.code === 200) {
                        // window.location.href = base + "/analyse/repairStructure/admin/main.html"
                        setTimeout(function () {
                            window.location.reload();
                        }, 500)
                    } else {

                    }
                }
            });
        })

        $("#del").on("click", function () {


            var idList = []

            idList.push($("#parentId").val())


            $.ajax({
                type: "GET",
                url: base + "/analyse/repairStructure/admin/delRepairStructure",
                data:{"datas":idList},
                dataType: "json",
                success: function (data) {

                    if(data.code===200){
                        layer.msg(data.message)

                            window.location.reload();

                    }else{
                        layer.msg(data.message)
                    }
                }
            });
        })


        $("#submit").on("click", function () {
            if (check() === false) {
                return;
            }

            var formDate = {
                structureName: $("#structureName").val(),
                parentId: $("#parentId").val()
            }
            $.ajax({
                type: "POST",
                url: base + "/analyse/repairStructure/admin/addRepairStructure",
                data: formDate,
                success: function (data) {
                    layer.msg(data.message)
                    if (data.code === 200) {
                        // window.location.href = base + "/analyse/repairStructure/admin/main.html"
                        setTimeout(function () {
                            window.location.reload();
                        }, 1000)
                    } else {

                    }
                }
            });
        })








        function check() {
            if ($("#structureName").val().length < 1) {
                layer.msg("请输入结构名字");
                return false;
            }
            if ($("#parentId").val().length < 1) {
                layer.msg("请选择上级");
                return false;
            }
        }

        //加载树形插件数据
        function getTreeData() {
            var dt;
            $.ajax({
                type: "GET",
                async: false,
                url: base + "/analyse/repairStructure/admin/getTreeStructure",
                success: function (data) {
                    dt = data.data;
                    // console.log(dt)
                }
            });
            return dt;
        }


    });
</script>
</body>
</html>